package com.xq.chrome.eurekaconsumer.impl;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.xq.chrome.eurekaconsumer.ConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @author MACHENIKE
 */
@Service
public class ComsumerServiceImpl implements ConsumerService {
  /**
   * 自动装配
   */
  @Autowired
  private RestTemplate restTemplate;

  /**
   * 开启容错服务!
   *
   * @return
   */
  @HystrixCommand(fallbackMethod = "fallBack")
  @Override
  public String getPing() {
    return restTemplate.getForObject("http://provider/user/ping", String.class);
  }

  /**
   * 服务降级处理
   *
   * @return
   */
  public String fallBack() {
    return "error";
  }

  /**
   * 服务降级处理方式2
   *
   * @return
   */
  public String fallBack2(String id, Throwable e) {
    return e.getMessage();
  }

  /**
   * 开启容错服务!
   *
   * @return
   */
  @Override
  @HystrixCommand(fallbackMethod = "fallBack2")
  public String getPing2(String id) {
    if (true) {
      throw new RuntimeException("get ping2 is broke!");
    }
    return restTemplate.getForObject("http://provider/user/ping", String.class);
  }

}
